//
//  DACommentView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/23.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DACommentView.h"
#import "DALabel.h"
#import "DACommentTableViewCell.h"
#import "Utility.h"
#import "UIViewAdditions.h"
#import "DAReplyModel.h"
@interface DACommentView ()<UITableViewDataSource, UITableViewDelegate>
{
    UIButton *moreCommentButton;
    UILabel *lineLabel;
    BOOL isKeyboardShowing;
}
@property (nonatomic, strong) UITableView *CommenttableView;
@property (nonatomic, copy) NSString *replayName;
@property (nonatomic, copy) NSString *replayUserID;
@property (nonatomic, strong) NSMutableArray *array;

@end
@implementation DACommentView

//- (UITableView *)CommenttableView{
//    if (!_CommenttableView) {
//        _CommenttableView = [[UITableView alloc]init];
//        _CommenttableView.scrollEnabled = NO;
//        _CommenttableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _CommenttableView.layer.cornerRadius = 5;
//        // 注册通知
//       // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"COMMENTTABLEVIEWRELOAD" object:nil];
//        [self addSubview:_CommenttableView];
//    }
//    return _CommenttableView;
//}

- (void)awakeFromNib{
        self.CommenttableView = [[UITableView alloc]init];
        self.CommenttableView.scrollEnabled = NO;
        self.CommenttableView.scrollsToTop = NO;
        self.CommenttableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.CommenttableView.dataSource = self;
        self.CommenttableView.delegate = self;
        [self addSubview:self.CommenttableView];
    self.isClickMoreDataButton = YES;

}



- (void)reloadData: (NSNotification *)notif{
    self.commentArray = notif.object;
    [self.CommenttableView reloadData];
}

//- (void)creatView{
//    [self removeAllSubviews];
//    self.CommenttableView = [[UITableView alloc]init];
//    self.CommenttableView.scrollEnabled = NO;
//    self.CommenttableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.CommenttableView.layer.cornerRadius = 5;
//    self.CommenttableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 100, self.sumHeight);
//    [self addSubview:self.CommenttableView];
//    self.CommenttableView.dataSource = self;
//    self.CommenttableView.delegate = self;
//    
//}

- (void)creatMoreComment:(NSMutableArray *)array{
    [moreCommentButton removeFromSuperview];
    [lineLabel removeFromSuperview];
    if (array.count > 0 ) {
        if (self.isClickMoreDataButton) {
            moreCommentButton = [UIButton buttonWithType:UIButtonTypeCustom];
            moreCommentButton.backgroundColor = [UIColor whiteColor];
            moreCommentButton.frame = CGRectMake(self.CommenttableView.frame.origin.x, self.CommenttableView.frame.origin.y + self.CommenttableView.frame.size.height + 0.5, self.CommenttableView.frame.size.width, 30);
            NSString *moreComment = NSLocalizedString(@"moreCommentData", nil);
            [moreCommentButton setTitle:moreComment forState:UIControlStateNormal];
            moreCommentButton.titleLabel.font = [UIFont systemFontOfSize:12];
            [moreCommentButton setTitleColor:[UIColor supportGreenColor] forState:UIControlStateNormal];
            [moreCommentButton addTarget:self action:@selector(clickMoreComment) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:moreCommentButton];
            lineLabel = [[UILabel alloc]init];
            lineLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
            lineLabel.frame = CGRectMake(self.CommenttableView.frame.origin.x, self.CommenttableView.frame.origin.y + self.CommenttableView.frame.size.height, self.CommenttableView.frame.size.width, 0.5);
            [self addSubview:lineLabel];
        }
    }
}
#pragma mark - 点击更多评论
- (void)clickMoreComment{
    // 发送通知显示全部评论
    // 把点击的cellIndexPath传回去
    self.isClickMoreDataButton = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REPLYCOMMENTUPDATE" object:self.cellIndexPath];
    // 移除line 和显示更多按钮
    [lineLabel removeFromSuperview];
    [moreCommentButton removeFromSuperview];
    //[self.CommenttableView reloadData];
}
-  (void)transferCommetArray:(NSMutableArray *)commtentArray indexPath:(NSIndexPath *)indexPath{
    self.CommenttableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 100, self.sumHeight);
    self.commentArray = [NSMutableArray array];
    self.commentArray = commtentArray;
    self.array = [NSMutableArray array];
    if (self.isShowAllReply) {
        self.array = self.commentArray;
        self.isClickMoreDataButton = NO;
    }else{
        if (commtentArray.count > 0) {
            if (commtentArray.count > 5) {
                for (int i = 0; i< 5; i++) {
                    [self.array addObject:commtentArray[i]];
                }
            }else{
                self.array = self.commentArray;
                self.isClickMoreDataButton = NO;
            }
        }
    }
    
    [self creatMoreComment:commtentArray];
    self.cellIndexPath = indexPath;
    [self.CommenttableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.commentArray.count > 0) {
        //return self.commentArray.count;
        return self.array.count;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DACommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    if (cell == nil) {
        cell = [[DACommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"commentCell"];
    }
    if (self.array.count > 0) {
        cell.replyModel = self.array[indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.commentArray.count > 0) {
        CGFloat sumHeight = [self.array[indexPath.row] rowHeight] + 10;
        return sumHeight;
    }else{
        return 0;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DACommentTableViewCell *cell =[self.CommenttableView cellForRowAtIndexPath:indexPath];
    CGFloat height = [cell convertRect:cell.bounds toView:self.viewForFirstBaselineLayout.window].origin.y + cell.frame.size.height;
    
    NSString *replyHeight = [NSString stringWithFormat:@"%f", height];
    NSString *isBackCell = [NSString stringWithFormat:@"%ld", self.isBackCell];
    // 点击回复
   // self.replayName = self.commentArray[indexPath.row][@"replyUserName"];
    self.replayName = [self.commentArray[indexPath.row] replyUserName];
    //self.replayUserID = self.commentArray[indexPath.row][@"replyUserID"];
    self.replayUserID = [self.commentArray[indexPath.row] replyUserID];
    if (self.replayUserID == nil) {
        // self.replayUserID = self.commentArray[indexPath.row][@"userID"];
        self.replayUserID = [self.commentArray[indexPath.row] userID];
    }
    NSDictionary *dic = @{@"replayUserID": self.replayUserID,
                          @"replayName": self.replayName,
                          @"replytTimeline": self.timeLineID,
                          @"cellIndexPath": self.cellIndexPath,
                          @"replyHeight": replyHeight,
                          @"isBackCell": isBackCell};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"transferReplyID" object:dic];
    // 发送通知 让键盘弹起
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REPLYCOMMENT" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGEPLACEHOLDER" object:self.replayName];
  
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"COMMENTTABLEVIEWRELOAD" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"transferReplyID" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"REPLYCOMMENT" object:nil];
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
